//
//  ChangePasswordViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/23.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "WinkAlertView.h"
#import "ValidateText.h"
#import "TelephonyInfo.h"
@interface ChangePasswordViewController ()<UIAlertViewDelegate>
{
    AFHTTPRequestOperationManager *afManager;
}
@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(IBAction)changePasswordEvent:(id)sender
{
    
    
    //檢查有沒有輸入密碼
    if (self.TF_OldText.text.length >0 && self.TF_NewText.text.length >0 &&self.TF_NewTextAffirm.text.length >0) {
        
        if ([self.TF_OldText.text isEqualToString:self.TF_NewText.text]) {
            
            [WinkAlertView _showAlertViewWithTitle:@"新舊密碼一樣" content:@"" cancelButtonTitle:@"確認" showTime:2.0f];

        //檢查兩組密碼是否一樣
        }else if ([ValidateText validatePassword:self.TF_NewText.text]&&[self.TF_NewText.text isEqualToString:self.TF_NewTextAffirm.text])
        {
            if (self.AV_ChangePasswordLoading == nil) {
                self.AV_ChangePasswordLoading = [[UIAlertView alloc]initWithTitle:@"變更密碼" message:@"請等待..." delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
            }
            [self.AV_ChangePasswordLoading show];
            [self sendOldPassword:self.TF_OldText.text andNewPassword:self.TF_NewText.text];
            
        }else if (![ValidateText validatePassword:self.TF_NewText.text]){
            
            [WinkAlertView _showAlertViewWithTitle:@"密碼格式不正確" content:@"" cancelButtonTitle:@"確認" showTime:2.0f];
            
        }else if (![self.TF_NewText.text isEqualToString:self.TF_NewText.text])
        {
            [WinkAlertView _showAlertViewWithTitle:@"密碼不相同" content:@"" cancelButtonTitle:@"確認" showTime:2.0f];
            
        }else
        {
            NSLog(@"12333");
        }
        
    }else{
        [WinkAlertView _showAlertViewWithTitle:@"請輸入密碼" content:@"" cancelButtonTitle:@"確認" showTime:2.0f];
    }

}
-(void)sendOldPassword:(NSString *)oldPassword andNewPassword:(NSString *)newPassword
{
    afManager = [AFHTTPRequestOperationManager manager];
    
    afManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    afManager.responseSerializer.acceptableContentTypes = [afManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
    
    //取出國碼加帳號
    NSString *account = [TelephonyInfo accountAddCountryCode];
    
    
    NSDictionary *parameters = @{@"type":@"changepwd",@"secret":@"openfire",@"username":account,@"oldpwd":oldPassword,@"newpwd":newPassword};
    
    NSLog(@"%@",parameters);
    
    //取得token
    [afManager POST:MD_SERVER_API parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSLog(@"JSON: %@", responseObject);
        
        
        NSString *responseResult = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
        
        //成功
        if ([responseResult isEqualToString:@"0"]) {
            
            [self.AV_ChangePasswordLoading dismissWithClickedButtonIndex:0 animated:YES];
            //儲存密碼
            [[NSUserDefaults standardUserDefaults]setValue:newPassword forKey:@"password"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            [WinkAlertView _showAlertViewHaveDelegateWithTitle:@"成功" content:@"密碼變更完成" cancelButtonTitle:@"OK" showTime:3.0 delegate:self];
        }else
        {
            [self.AV_ChangePasswordLoading dismissWithClickedButtonIndex:0 animated:YES];

            [WinkAlertView _showAlertViewWithTitle:@"失敗" content:responseObject[@"msg"] cancelButtonTitle:@"確認" showTime:3.0f];
            
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        
        [self.AV_ChangePasswordLoading dismissWithClickedButtonIndex:0 animated:YES];

        [WinkAlertView _showAlertViewWithTitle:@"失敗" content:@"網路連結失敗" cancelButtonTitle:@"確認" showTime:3.0f];
        
        
    }];
    
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
